package com.lottery.material.controller.admin.banner;

import com.lottery.material.common.EnumInfo;
import com.lottery.material.controller.base.BaseController;
import com.lottery.material.enumeration.ResultEnum;
import com.lottery.material.enumeration.common.CommonEnum;
import com.lottery.material.enumeration.conf.MenuLevelConfigEnum;
import com.lottery.material.pojo.banner.BannerConf;
import com.lottery.material.pojo.banner.BannerJumpTypeDict;
import com.lottery.material.pojo.conf.MenuLevelConfig;
import com.lottery.material.pojo.page.Page;
import com.lottery.material.pojo.page.PageData;
import com.lottery.material.pojocustom.banner.BannerConfCustom;
import com.lottery.material.service.admin.banner.BannerConfService;
import com.lottery.material.service.admin.banner.BannerJumpTypeDictService;
import com.lottery.material.service.admin.conf.MenuLevelConfigService;
import com.lottery.material.util.EnumUtil;
import com.lottery.material.util.Jurisdiction;
import com.lottery.material.util.PdUtil;
import com.lottery.material.util.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * Description：banner配置Controller
 * Created by 朱正磊 on 2019-04-13 20:06
 */
@Controller("adminBannerConfController")
@RequestMapping("/admin/bannerConf")
public class BannerConfController extends BaseController {

    //菜单地址，用于权限控制
    private String menuUrl = "admin/bannerConf/list";

    private final BannerConfService bannerConfService;

    private final BannerJumpTypeDictService bannerJumpTypeDictService;

    private final MenuLevelConfigService menuLevelConfigService;

    @Autowired
    public BannerConfController(BannerConfService bannerConfService,
                                BannerJumpTypeDictService bannerJumpTypeDictService,
                                MenuLevelConfigService menuLevelConfigService) {
        this.bannerConfService = bannerConfService;
        this.bannerJumpTypeDictService = bannerJumpTypeDictService;
        this.menuLevelConfigService = menuLevelConfigService;
    }

    /**
     * description：分页列表
     * user 朱正磊
     * time 2019-04-17 15:06
     * @param page 分页对象
     * @param map 响应数据Map
     * @return 返回模型视图
     */
    @RequestMapping("/list")
    public ModelAndView listPage(Page page, Map<String, Object> map) {
        //分页对象设置页面请求参数
        PageData pd = getPageData();
        PdUtil.rePut(pd);
        page.setPd(pd);

        //查询分页列表
        List<BannerConfCustom> list = bannerConfService.findCustomListPage(page);
        map.put("list", list);
        map.put("pd", pd);
        map.put("QX", Jurisdiction.getHC());

        //状态（1：上架；2：下架）
        List<EnumInfo> stateList = EnumUtil.getEnumInfoList(CommonEnum.STATE, CommonEnum.class);
        map.put("stateList", stateList);

        return new ModelAndView("banner/bannerConf/listPage", map);
    }

    /**
     * description：详情页面
     * user 朱正磊
     * time 2019-04-17 16:20
     * @param id 主键
     * @param map 响应数据Map
     * @return 返回模型视图
     */
    @RequestMapping("/detailPage")
    public ModelAndView detailPage(Integer id, Map<String, Object> map) {
        if(!Jurisdiction.buttonJurisdiction(menuUrl, "cha")) {//判断是否有查看权限
            return null;
        }

        //根据主键查询扩展类
        BannerConfCustom bannerConf = bannerConfService.findCustomById(id);
        map.put("bannerConf", bannerConf);

        return new ModelAndView("banner/bannerConf/detailPage", map);
    }

    /**
     * description：添加页面
     * user 朱正磊
     * time 2019-04-17 16:26
     * @param map 响应数据Map
     * @return 返回模型视图
     */
    @RequestMapping("/addPage")
    public ModelAndView addPage(Map<String, Object> map) {
        if(!Jurisdiction.buttonJurisdiction(menuUrl, "add")) {//判断是否有添加权限
            return null;
        }

        //跳转类型列表
        List<BannerJumpTypeDict> bannerJumpTypeDictList = bannerJumpTypeDictService.findAll();
        map.put("bannerJumpTypeDictList", bannerJumpTypeDictList);

        //所属模块列表（一级类别）
        List<MenuLevelConfig> menuLevelConfigList = menuLevelConfigService
                .findByLevelType((Integer) MenuLevelConfigEnum.LEVEL_TYPE_ONE.getValue());
        map.put("menuLevelConfigList", menuLevelConfigList);

        //添加请求地址
        map.put("actionUrl", "admin/bannerConf/add");

        return new ModelAndView("banner/bannerConf/editPage", map);
    }

    /**
     * description：添加
     * user 朱正磊
     * time 2019-04-17 16:39
     * @param request 请求对象
     * @param bannerConf 对象
     * @param map 响应数据Map
     * @return 返回模型视图
     */
    @RequestMapping("/add")
    public ModelAndView add(HttpServletRequest request, BannerConf bannerConf, Map<String, Object> map) {
        Integer flag = bannerConfService.add(request, bannerConf);
        if (Objects.equals(flag, 0)) { //添加失败
            throw new RuntimeException(ResultEnum.STATUS_PERSISTENT_OPERATE_SQL_FAIL.getMessage());
        } else {
            map.put("msg","success");
            return new ModelAndView("save_result", map);
        }
    }

    /**
     * description：修改页面
     * user 朱正磊
     * time 2019-04-17 16:48
     * @param id 主键
     * @param map 响应数据Map
     * @return 返回模型视图
     */
    @RequestMapping("/editPage")
    public ModelAndView editPage(Integer id, Map<String, Object> map) {
        if(!Jurisdiction.buttonJurisdiction(menuUrl, "edit")) {//判断是否有修改权限
            return null;
        }

        //根据主键查询扩展类
        BannerConfCustom bannerConf = bannerConfService.findCustomById(id);
        map.put("bannerConf", bannerConf);

        //跳转类型列表
        List<BannerJumpTypeDict> bannerJumpTypeDictList = bannerJumpTypeDictService.findAll();
        map.put("bannerJumpTypeDictList", bannerJumpTypeDictList);

        //所属模块列表（一级类别）
        List<MenuLevelConfig> menuLevelConfigList = menuLevelConfigService
                .findByLevelType((Integer) MenuLevelConfigEnum.LEVEL_TYPE_ONE.getValue());
        map.put("menuLevelConfigList", menuLevelConfigList);

        //修改请求地址
        map.put("actionUrl", "admin/bannerConf/edit");

        return new ModelAndView("banner/bannerConf/editPage", map);
    }

    /**
     * description：修改
     * user 朱正磊
     * time 2019-04-17 16:58
     * @param request 请求对象
     * @param bannerConf 对象
     * @param map 响应数据Map
     * @return 返回模型视图
     */
    @RequestMapping("/edit")
    public ModelAndView edit(HttpServletRequest request, BannerConf bannerConf, Map<String, Object> map) {
        Integer flag = bannerConfService.updateById(request, bannerConf);
        if (Objects.equals(flag, 0)) { //修改失败
            throw new RuntimeException(ResultEnum.STATUS_PERSISTENT_OPERATE_SQL_FAIL.getMessage());
        } else {
            map.put("msg","success");
            return new ModelAndView("save_result", map);
        }
    }

    /**
     * description：上架或者下架
     * user 朱正磊
     * time 2019-05-21 15:56
     * @param id 主键id
     * @param state 上下架：1上架；2下架
     * @return 返回响应对象
     */
    @RequestMapping("/onOrOff")
    @ResponseBody
    public ResponseResult onOrOff(Integer id, Integer state) {
        BannerConf bannerConf = bannerConfService.findById(id);
        final String name = "该banner";
        if (Objects.equals(CommonEnum.STATE_ON.getValue(), state)) { //进行下架
            bannerConf.setState((Integer) CommonEnum.STATE_OFF.getValue());
            Integer flag = bannerConfService.updateById(null, bannerConf);
            if (flag == 0) {
                return ResponseResult.buildFormatResult(ResultEnum.ADMIN_OFF_FAILURE, name);
            }
            return ResponseResult.buildFormatResult(ResultEnum.ADMIN_OFF_SUCCESS, name);
        } else { //进行上架
            bannerConf.setState((Integer) CommonEnum.STATE_ON.getValue());
            Integer flag = bannerConfService.updateById(null, bannerConf);
            if (flag == 0) {
                return ResponseResult.buildFormatResult(ResultEnum.ADMIN_ON_FAILURE, name);
            }
            return ResponseResult.buildFormatResult(ResultEnum.ADMIN_ON_SUCCESS, name);
        }
    }

}
